#ilms/lib/simulation_functions.rb

class SimulationFunctions < ApplicationController
#declarations
	def initialize
		@outlets = OutletStatus.get_outlet_status
		@user = UserSetting.get_user_settings
		#@utility = UtilitySetting.last
		@green = 0
		@power = 0
		@avg_percent = 0
	end
#utility functions

#simulate power consumption
	def get_sim_power
		powerSim = 0.0
		if @outlets[0].status then
			powerSim = powerSim + 200.0
		end
		if @outlets[1].status then
			powerSim = powerSim + 300.0
		end
		if @outlets[2].status then
			powerSim = powerSim + 150.0
		end
		if @outlets[3].status then
			powerSim = powerSim + 150.0
		end
		if @outlets[4].status then
			powerSim = powerSim + 75.0
		end
		return powerSim
	end


#keep track of time
	def time_tracker
		
	end

	def run_algorithmPastFive
	   data = []
	   preference = @preference
	   util_cntrl_lvl = @user.utility_control_lvl
	   #or_lvl = @utility.override_lvl
	   avg_percent = @avg_percent
	   for outlet in @outlets
			case preference
			when preference = 1
			 if outlet.priority != 1 and outlet.status == true
				data << outlet.outlet_id
				outlet.status = false
			 elsif outlet.priority == 1 and outlet.status == false
				data << outlet.outlet_id
				outlet.status = true
			 end
			when preference = 2
			  if avg_percent >= 0.8 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				outlet.status = true
			  elsif avg_percent >= 1 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				outlet.status = true
			  elsif avg_percent < 0.8 and outlet.priority == 2 and outlet.status == true
				data << outlet.outlet_id
				outlet.status = false
			  elsif avg_percent < 1 and outlet.priority == 3 and outlet.status == true
				data << outlet.outlet_id
				outlet.status = false
			  end
			when preference = 3
			  if avg_percent >= 0.4 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				outlet.status = true
			  elsif avg_percent >= 0.6 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				outlet.status = true
			  elsif avg_percent < 0.4 and outlet.priority > 1 and outlet.status == true
				data << outlet.outlet_id
				outlet.status = false
			  end
			when preference = 4
			  if avg_percent >= 0.1 and outlet.priority < 3 and outlet.status == false
				data << outlet.outlet_id
				outlet.status = true
			  elsif avg_percent >= 0.3 and outlet.priority == 3 and outlet.status == false
				data << outlet.outlet_id
				outlet.status = true
			  elsif avg_percent < 0.1 and outlet.priority > 1 and outlet.status == true
				data << outlet.outlet_id
				outlet.status = false
			  end
			when preference = 5
				if outlet.status == false
				  data << outlet.outlet_id
				  outlet.status = true
				end
			when preference = 6
				if outlet.priority < 3 and outlet.status == false
				  data << outlet.outlet_id
				  outlet.status = true
				elsif outlet.priority == 3 and outlet.status == true
				  data << outlet.outlet_id
				  outlet.status = false
				end
			end
		 end
	    return data
	end
	
	def simulate_cycle
	    cycle_data = ""
		current_sim_time = Time.at(0).utc
		power = 0
		@small_cycle_power = 0
		for hours in 1..24
			for half_hours in 1..2
				t = current_sim_time.hour
				@green = GreenSimulation.where(:sim_hour => t).last
				#if first round calculated, give it a base power sim number
				#also making sure small_cycle_power is never zero
				if @small_cycle_power == 0
					@small_cycle_power = get_sim_power
				end
				@avg_percent = @green.power/@small_cycle_power
				data = run_algorithmPastFive
				@small_cycle_power = 0
				for i in 1..6
					power_inst = get_sim_power
					power = power + power_inst - @green.power	
					current_sim_time = current_sim_time + 300
					cycle_data << "," + power.to_s
					@small_cycle_power = @small_cycle_power + power
				end
				@small_cycle_power = @small_cycle_power / 6
			end
		end
		return cycle_data
	end
	
	def simulate_pref(pref)
		File.open("simTest.csv", "w+") do |f|
			for i in 1..pref
				@preference = i
				
				data = simulate_cycle
			    if i == pref
					f << "Preference#{i}#{data}"
				else
					f << "Preference#{i}#{data}\n"
				end
			end
		end	
	end
	
	def Csv_cleanse
	    counter = 0
		File.open("ilms2.csv", "r") do |f|
			while (line = f.gets)
				puts "#{counter}: #{line}"
				counter = counter + 1
			end
		end	
	end
end